import React from 'react';
import { PredictionResult } from '../../types';

interface PredictionTableProps {
  prediction: PredictionResult;
}

const PredictionTable: React.FC<PredictionTableProps> = ({ prediction }) => {
  if (!prediction || !prediction.predictedValues || prediction.predictedValues.length === 0) {
    return (
      <div className="bg-white rounded-lg border border-gray-200 p-6 text-center shadow-sm">
        <p className="text-gray-500">No prediction results available</p>
      </div>
    );
  }

  // Sort predictions by probability (descending)
  const sortedPredictions = [...prediction.predictedValues]
    .sort((a, b) => b.probability - a.probability);

  // Get the top prediction and its probability
  const topPrediction = sortedPredictions[0];
  const maxProbability = topPrediction.probability;

  return (
    <div className="bg-white rounded-lg border border-gray-200 p-4 shadow-sm">
      <h3 className="text-lg font-medium text-gray-800 mb-4">Prediction Results</h3>
      
      <div className="mb-4 p-4 bg-blue-50 border border-blue-100 rounded-lg">
        <div className="text-sm text-blue-700 mb-1">Top Prediction</div>
        <div className="flex items-center justify-between">
          <div className="flex items-center">
            <div className="w-12 h-12 flex items-center justify-center bg-blue-600 text-white rounded-lg text-2xl font-bold">
              {topPrediction.value}
            </div>
            <div className="ml-4">
              <div className="text-lg font-medium">Next Number</div>
              <div className="text-sm text-gray-500">With {(topPrediction.probability * 100).toFixed(1)}% confidence</div>
            </div>
          </div>
        </div>
      </div>
      
      <div className="overflow-x-auto">
        <table className="min-w-full divide-y divide-gray-200">
          <thead className="bg-gray-50">
            <tr>
              <th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                Value
              </th>
              <th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                Probability
              </th>
              <th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                Confidence
              </th>
            </tr>
          </thead>
          <tbody className="bg-white divide-y divide-gray-200">
            {sortedPredictions.map((pred, index) => {
              // Calculate relative confidence for visualization
              const relativeConfidence = pred.probability / maxProbability;
              
              return (
                <tr key={index} className={index === 0 ? 'bg-blue-50' : ''}>
                  <td className="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                    {pred.value}
                  </td>
                  <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                    {(pred.probability * 100).toFixed(2)}%
                  </td>
                  <td className="px-6 py-4 whitespace-nowrap">
                    <div className="w-full bg-gray-200 rounded-full h-2.5">
                      <div
                        className="bg-blue-600 h-2.5 rounded-full"
                        style={{ width: `${relativeConfidence * 100}%` }}
                      ></div>
                    </div>
                  </td>
                </tr>
              );
            })}
          </tbody>
        </table>
      </div>
    </div>
  );
};

export default PredictionTable;